﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using JXT.PrimaryKey.Batman.Reflection;
using JXT.PrimaryKey.Batman.Language;

namespace JXT.PrimaryKey.Batman.WcfService.Models
{
    public static class ModelExtensions
    {
        public static Queue<KeyValuePair<string, DisplayColumnInfo>> ToDisplayColumnQueue(this Type modelType, ILanguage language = null)
        {
            Queue<KeyValuePair<string, DisplayColumnInfo>> result = new Queue<KeyValuePair<string, DisplayColumnInfo>>();
            DisplayComparer displayComparer = new DisplayComparer();
            var orderdMetaList = modelType.GetMetadataCacheItems().OrderBy(arg1 => arg1.Attributes.OfType<DisplayAttribute>().FirstOrDefault(), displayComparer).ToList();
            foreach (var item in orderdMetaList)
            {
                var displayAttribute = item.Attributes.OfType<DisplayAttribute>().FirstOrDefault();
                if (displayAttribute == null)
                    continue;
                DisplayColumnInfo info = new DisplayColumnInfo();
                info.PropertyName = item.Property.Name;
                info.Order = displayAttribute.Order;
                if (language == null)
                {
                    info.Name = displayAttribute.Name;
                    info.ShortName = displayAttribute.ShortName;
                    info.Description = displayAttribute.Description;
                    info.GroupName = displayAttribute.GroupName;
                    info.Prompt = displayAttribute.Prompt;
                }
                else
                {
                    info.Name = language.Localize(displayAttribute.Name, displayAttribute.Name);
                    info.ShortName = language.Localize(displayAttribute.ShortName, displayAttribute.ShortName);
                    info.Description = language.Localize(displayAttribute.Description, displayAttribute.Description);
                    info.GroupName = language.Localize(displayAttribute.GroupName, displayAttribute.GroupName);
                    info.Prompt = language.Localize(displayAttribute.Prompt, displayAttribute.Prompt);
                }
                //DisplayFormat
                var formatAttribute = item.Attributes.OfType<DisplayFormatAttribute>().FirstOrDefault();
                if (formatAttribute != null)
                {
                    info.FormatString = formatAttribute.DataFormatString;
                    info.HtmlEncode = formatAttribute.HtmlEncode;
                    info.NullDisplayText = formatAttribute.NullDisplayText;
                }
                result.Enqueue(new KeyValuePair<string, DisplayColumnInfo>(item.Property.Name, info));
            }
            return result;
        }

        public class DisplayComparer : IComparer<DisplayAttribute>
        {
            public int Compare(DisplayAttribute x, DisplayAttribute y)
            {
                if (x == null && y == null)
                    return 0;
                if (x == null)
                    return 1;
                if (y == null)
                    return -1;
                return x.Order - y.Order;
            }
        }
    }
}
